package steed.domain.wechat;

import java.io.UnsupportedEncodingException;
import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

import com.vdurmont.emoji.EmojiParser;

import steed.exception.runtime.system.FrameworkException;
import steed.hibernatemaster.annotation.FuzzyQuery;
import steed.hibernatemaster.annotation.FuzzyQuery.FuzzyQuerystrategy;
import steed.hibernatemaster.domain.BaseRelationalDatabaseDomain;
import steed.hibernatemaster.util.DaoUtil;
import steed.util.base.StringUtil;
import steed.util.digest.Base64Util;
import steed.util.wechat.domain.result.WechatResult;

/**
 * 微信用户
 * @author 战马
 *
 */
@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class WechatUser extends BaseRelationalDatabaseDomain implements WechatResult{
	private static final long serialVersionUID = 1L;
	private String errcode;
	private String errmsg;
	private String openid;
	/**
	 * 用户的性别，值为1时是男性，值为2时是女性，值为0时是未知 
	 */
	private Integer sex;
	
	private String nickname;
	private String nicknameWhithEmoj;
	private String province;
	private String city;
	private String country;
	private String headimgurl;
	private String unionid;
	private String[] privilege;
	private String privilegeStr;
	/**
	 * 用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息。
	 */
	private Integer subscribe;
	private Long subscribe_time;
	private Integer groupid;
	private String remark;
	private Double integration;
	@FuzzyQuery(FuzzyQuerystrategy.none)
	private String wechatAccountAppid;
	private WechatAccount wechatAccount;
	
	/**
	 * 用户关注的渠道来源，
	 * ADD_SCENE_SEARCH 公众号搜索，
	 * ADD_SCENE_ACCOUNT_MIGRATION 公众号迁移，
	 * ADD_SCENE_PROFILE_CARD 名片分享，
	 * ADD_SCENE_QR_CODE 扫描二维码，
	 * ADD_SCENEPROFILE LINK 图文页内名称点击，
	 * ADD_SCENE_PROFILE_ITEM 图文页右上角菜单，
	 * ADD_SCENE_PAID 支付后关注，
	 * ADD_SCENE_OTHERS 其他
	 */
	private String subscribe_scene;
	
	@Id
	@GenericGenerator(name="gen1",strategy="assigned")
	@GeneratedValue(generator="gen1")
	public String getOpenid() {
		return openid;
	}
	
	public WechatUser(String openid) {
		super();
		this.openid = openid;
	}

	@Transient
	public WechatAccount getWechatAccount() {
		if (wechatAccount == null && !StringUtil.isStringEmpty(wechatAccountAppid)) {
			wechatAccount = DaoUtil.get(WechatAccount.class, wechatAccountAppid);
		}
		return wechatAccount;
	}
	
	public WechatUser() {
		super();
	}
	
	/**
	 * 获取用户关注的渠道来源(中文)
	 */
	@Transient
	public String subscribeSceneChinese(){
		if (StringUtil.isStringEmpty(subscribe_scene)) {
			return "未知";
		}
		switch (subscribe_scene) {
		 case "ADD_SCENE_SEARCH":
			return "公众号搜索";
		 case "ADD_SCENE_ACCOUNT_MIGRATION":
			 return "公众号迁移";
		 case "ADD_SCENE_PROFILE_CARD":
			 return "名片分享";
		 case "ADD_SCENE_QR_CODE":
			 return "扫描二维码";
		 case "ADD_SCENEPROFILE LINK":
			 return "图文页内名称点击";
		 case "ADD_SCENE_PROFILE_ITEM":
			 return "图文页右上角菜单";
		 case "ADD_SCENE_PAID":
			 return "支付后关注";
		 case "ADD_SCENE_OTHERS":
			 return "其他";
		 default:
			 return "未知";
		}
	}

	public Integer getSubscribe() {
		return subscribe;
	}

	public String getSubscribe_scene() {
		return subscribe_scene;
	}

	public void setSubscribe_scene(String subscribe_scene) {
		this.subscribe_scene = subscribe_scene;
	}
	
	@Transient
	public Date getSubscribeDate(){
		if (subscribe_time != null) {
			return new Date(subscribe_time*1000);
		}
		return null;
	}

	@Column(length=32)
	public String getWechatAccountAppid() {
		return wechatAccountAppid;
	}

	public void setWechatAccountAppid(String wechatAccountAppid) {
		this.wechatAccountAppid = wechatAccountAppid;
	}

	public Long getSubscribe_time() {
		return subscribe_time;
	}

	public void setSubscribe_time(Long subscribe_time) {
		this.subscribe_time = subscribe_time;
	}

	@Column(length=255)
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Integer getGroupid() {
		return groupid;
	}

	public void setGroupid(Integer groupid) {
		this.groupid = groupid;
	}

	public void setSubscribe(Integer subscribe) {
		this.subscribe = subscribe;
	}

	public Double getIntegration() {
		return integration;
	}

	public void setIntegration(Double integration) {
		this.integration = integration;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public Integer getSex() {
		return sex;
	}
	@Transient
	public String getErrcode() {
		return errcode;
	}

	public void setErrcode(String errcode) {
		this.errcode = errcode;
	}
	@Transient
	public String getErrmsg() {
		return errmsg;
	}
	public void setErrmsg(String errmsg) {
		this.errmsg = errmsg;
	}

	public String getNickname() {
		return nickname;
	}
	
	@Transient
	public String getNicknameWhithEmoj() {
		if (nicknameWhithEmoj == null && nickname != null) {
			nicknameWhithEmoj = getNicknameWhithEmoj(nickname);
		}
		return nicknameWhithEmoj;
	}

	public String getNicknameWhithEmoj(String encodedNickname) {
		try {
			return Base64Util.base64Decode(encodedNickname);
		} catch (Exception e) {
		}
		return encodedNickname;
	}
	
	/*@Transient
	public String getDecodeNickname() {
		if (!StringUtil.isStringEmpty(nickname) && nickname.startsWith("#base64#")) {
			return Base64Util.base64Decode(nickname.replace("#base64#", ""));
		}
		return nickname;
	}*/

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	private String praseNickname() {
		if (!StringUtil.isStringEmpty(nickname)) {
			nickname = Base64Util.base64Encode(nickname);
		}
		return nickname;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getHeadimgurl() {
		return headimgurl;
	}
	@Transient
	public String getLargeHeadimgurl() {
		if (StringUtil.isStringEmpty(headimgurl)) {
			return "";
		}
		if ("/132".equals((headimgurl.substring(headimgurl.length()-4)))) {
			return headimgurl.substring(0, headimgurl.length()-4)+"/0";
		}
		return headimgurl;
	}

	public void setHeadimgurl(String headimgurl) {
		this.headimgurl = headimgurl;
	}

	public String getUnionid() {
		return unionid;
	}

	public void setUnionid(String unionid) {
		this.unionid = unionid;
	}
	@Transient
	public String[] getPrivilege() {
		return privilege;
	}

	public void setPrivilege(String[] privilege) {
		this.privilege = privilege;
		if (privilege == null) {
			privilegeStr = null;
		}else{
			if (privilege.length > 0) {
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < privilege.length; i++) {
					sb.append(privilege[i]).append(",");
				}
				privilegeStr = sb.substring(0, sb.length() - 1);
			}else {
				privilegeStr = "";
			}
		}
	}

	public String getPrivilegeStr() {
		return privilegeStr;
	}

	public void setPrivilegeStr(String privilegeStr) {
		this.privilegeStr = privilegeStr;
		if (privilegeStr == null) {
			privilege = null;
		}else {
			privilege = privilegeStr.split(",");
		}
	}

	public void setSex(Integer sex) {
		this.sex = sex;
	}

	@Override
	public void initializeAll() {
		initialize();
	}
	
	
	
	@Override
	public boolean save() {
		praseNickname();
		return super.save();
	}
	
	

	@Override
	public boolean update() {
		praseNickname();
		return super.update();
	}

	@Override
	@Transient
	public boolean isSuccess() {
		return StringUtil.isStringEmpty(errcode);
	}
}
